package top.jacktgq.itheima.netty.protocol_design.http;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author CandyWall
 * @Date 2021/3/24--1:34
 * @Description
 */
@Slf4j
public class TestHttp {
    public static void main(String[] args) {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        NioEventLoopGroup workerGroup = new NioEventLoopGroup(2);
        ChannelFuture channelFuture = new ServerBootstrap()
                .group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline()
                                .addLast(new LoggingHandler(LogLevel.DEBUG))
                                .addLast(new HttpServerCodec())
                                .addLast(new SimpleChannelInboundHandler<HttpRequest>() {
                                    @Override
                                    protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
                                        // 获取请求的信息
                                        log.debug(msg.uri());

                                        // 返回响应
                                        DefaultFullHttpResponse response = new DefaultFullHttpResponse(msg.protocolVersion(), HttpResponseStatus.OK);
                                        byte[] bytes = "<h1 style='color:red'>Hello Http!</h1>".getBytes();
                                        // 响应头中写入响应内容长度
                                        response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, bytes.length);
                                        // 响应体
                                        response.content().writeBytes(bytes);
                                        // 写回响应
                                        ctx.writeAndFlush(response);
                                    }
                                });
                                /*.addLast(new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("{}", msg.getClass());
                                // 请求行，请求头
                                if (msg instanceof HttpRequest) {

                                }
                                // 请求体
                                else if(msg instanceof HttpContent) {

                                }
                            }
                        });*/
                    }
                })
                .bind(8080);
        // 监听服务器启动事件
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                log.debug("服务器启动成功...监听在8080端口");
                // 监听服务器ServerSocketChannel关闭事件，从而停止服务器端的事件循环组中的事件循环
                channelFuture.channel().closeFuture().addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        bossGroup.shutdownGracefully();
                        workerGroup.shutdownGracefully();
                    }
                });
            }
        });


    }
}
